// pages/mainOperation/message/message.js
//获取应用实例
const app = getApp()
let utils = require('../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    PageIndex: 1, //页码
    PageSize: 10, //页大小
    PageCount: 0, //总数量
    msgList: [], //消息列表
    tmpMsgId: 0, //临时存放消息id

    msgBadge: '', //消息提醒
    systemBadge: '', //系统通知
    currentTabIndex: 0, //当前被选中的tab索引
    messageType: 1, //消息类型
    noDataText: '暂无消息提醒', //暂无数据的消息提示文本
    noData: true, //是否显示暂无数据
    noDataHeight: '330', //暂无数据的高度
    modalHidden: true, //模态框隐藏
    modalMsg: '确认要删除消息吗？' //模态框中的信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    self.getMsgList(true);
  },
  /**
   *  tab切换
   */
  tabTap: function(e) {
    let self = this;
    let index = e.currentTarget.dataset.index;
    self.setData({
      currentTabIndex: index,
      messageType: e.currentTarget.dataset.mtype
    });
    self.setData({
      PageIndex: 1
    });
    self.getMsgList(true);
  },
  /**
   *  删除消息
   */
  delMsgTap: function(e) {
    let self = this;
    // 要删除消息的id
    let id = e.currentTarget.dataset.msgid;
    console.log(id)
    self.setData({
      modalHidden: false,
      tmpMsgId: e.currentTarget.dataset.id
    })
  },
  /**
   *  模态框组件的确认按钮
   */
  modalSureListener: function(e) {
    let self = this;
    //隐藏modal
    self.setData({
      modalHidden: true
    });
    let url = app.apiBaseUrl + "/Message/DeleteMessage";
    app.httpRequest({
      method: "POST",
      url: url,
      showLoading: true,
      data: {
        Id: self.data.tmpMsgId
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          wx.showToast({
            title: "删除成功"
          });
          self.setData({
            PageIndex: 1
          })
          self.getMsgList(false);
          self.getMessageTotalType();
        } else {
          wx.showToast({
            title: rdata.message
          });
        }
      }
    });
  },
  /**
   * 显示明细
   */
  gotoDetailTap: function(event) {
    let self = this;
    let mtype = event.currentTarget.dataset.mtype;
    let msgid = event.currentTarget.dataset.msgid;
    for (let i = 0; i < self.data.msgList.length; i++) {
      if (self.data.msgList[i].MessageID == msgid) {
        if (self.data.msgList[i].IsRead == 1) {
          self.data.msgList[i].IsRead = 2;
          self.setData({
            msgList: self.data.msgList
          });
        }
        break;
      }
    }


    let query = '?messageType=' + mtype + '&messageId=' + msgid;
    wx.navigateTo({
      url: "/pages/my/message/messageDetail/messageDetail" + query
    });
  },
  /**
   * 查询消息列表
   */
  getMsgList: function(showLoading) {
    let self = this;
    if (app.userInfo.UserID <= 0) {
      self.setData({
        msgList: [],
        noData: true
      });
      return;
    }
    let url = app.apiBaseUrl + "/Message/GetMessageTypePage";

    app.httpRequest({
      method: "GET",
      url: url,
      showLoading: showLoading,
      data: {
        userID: app.userInfo.UserID,
        messageType: self.data.messageType,
        PageIndex: self.data.PageIndex,
        PageSize: self.data.PageSize
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          if (self.data.PageIndex == 1) {
            //根据数据动态显示是否暂无数据
            if (rdata.data.length > 0) {
              //计算总页数
              let recordCount = parseInt(rdata.message);
              if (recordCount > 0) {
                self.data.PageCount = utils.evalPageCount(recordCount, self.data.PageSize);
              }
              self.setData({
                msgList: rdata.data,
                noData: false
              });
            } else {
              self.setData({
                msgList: [],
                noData: true
              });
            }
          } else {
            self.data.msgList.push(...rdata.data);
            self.setData({
              msgList: self.data.msgList
            });
          }
        } else {
          wx.showToast({
            title: rdata.message
          });
        }
      }
    });
  },
  getMessageTotalType: function() {
    let self = this;
    if (app.userInfo.UserID <= 0) {
      self.renderMessage([]);
      return;
    }
    let url = app.apiBaseUrl + "/Message/GetMessageTotalType";

    app.httpRequest({
      method: "GET",
      url: url,
      showLoading: false,
      data: {
        userID: app.userInfo.UserID
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          //如果返回成功,Message赋值总记录数	
          self.renderMessage(rdata.data);
        } else {
          wx.showToast({
            title: rdata.message
          });
        }
      }
    });
  },
  /**
   * 消息汇总
   * @param {Object} data
   */
  renderMessage: function(data) {
    let self = this;
    let opt = {
      msgBadge: '', //消息提醒数量
      systemBadge: '' //系统消息数量
    }
    if (data.length) {
      for (var i = 0; i < data.length; i++) {
        switch (data[i].MessageType) {
          case 1:
            opt.msgBadge = data[i].CT;
            break;
          case 2:
            opt.systemBadge = data[i].CT;
            break;
        }
      }
    }
    self.setData(opt);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let self = this;
    self.setData({
      PageIndex: 1
    });
    self.getMsgList(true);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let self = this;
    // 页面被拉到底部
    if (self.data.PageIndex < self.data.PageCount) {
      self.setData({
        PageIndex: self.data.PageIndex + 1
      });
      self.getMsgList(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})